package com.crane.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;

public final class PinyinTool {

	public static String getPinyin(String text, String separator) {
		char[] chars = text.toCharArray();
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		String[] s;
		String rs = "";
		try {
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < chars.length; i++) {
				if (String.valueOf(chars[i]).matches("[\\u4E00-\\u9FA5]+")) {
					s = PinyinHelper.toHanyuPinyinStringArray(chars[i], format);
					if (s != null) {
						sb.append(s[0]).append(separator);
						continue;
					}
				}
				sb.append(String.valueOf(chars[i]));
				if ((i + 1 >= chars.length) || String.valueOf(chars[i + 1]).matches("[\\u4E00-\\u9FA5]+")) {
					sb.append(separator);
				}
			}
			rs = sb.substring(0, sb.length() - 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 获取汉字首字母
	 * 
	 * @param text
	 * @return
	 */
	public static String getPinyinInitials(String text) {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < text.length(); i++) {
			char ch = text.charAt(i);
			String[] s = PinyinHelper.toHanyuPinyinStringArray(ch);
			if (s != null) {
				sb.append(s[0].charAt(0));
			} else {
				sb.append(ch);
			}
		}

		return sb.toString();
	}

	public static String convertHanzi2Pinyin(String hanzi, boolean full) {
		String regExp = "^[\u4E00-\u9FFF]+$";
		StringBuffer sb = new StringBuffer();
		if (hanzi == null || "".equals(hanzi.trim())) {
			return "";
		}
		String pinyin = "";
		for (int i = 0; i < hanzi.length(); i++) {
			char unit = hanzi.charAt(i);
			if (match(String.valueOf(unit), regExp))// 是汉字，则转拼音
			{
				pinyin = convertSingleHanzi2Pinyin(unit);
				if (full) {
					sb.append(pinyin);
				} else {
					sb.append(pinyin.charAt(0));
				}
			} else {
				sb.append(unit);
			}
		}
		return sb.toString();
	}

	private static String convertSingleHanzi2Pinyin(char hanzi) {
		HanyuPinyinOutputFormat outputFormat = new HanyuPinyinOutputFormat();
		outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		String[] res;
		StringBuffer sb = new StringBuffer();
		try {
			res = PinyinHelper.toHanyuPinyinStringArray(hanzi, outputFormat);
			sb.append(res[0]);// 对于多音字，只用第一个拼音
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return sb.toString();
	}

	public static boolean match(String str, String regex) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.find();
	}

	public static void main(String[] args) {
		System.out.println(PinyinTool.getPinyin("宋小雅", ""));
		System.out.println(PinyinTool.convertHanzi2Pinyin("宋小雅",true));
		System.out.println(PinyinTool.getPinyinInitials("宋小雅"));
	}

}
